package ar.com.fiuba;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;

import ar.com.fiuba.application.api.DataSource;
import ar.com.fiuba.domain.CoeficientCalculationStrategy;
import ar.com.fiuba.domain.Dictionary;
import ar.com.fiuba.domain.api.Corrector;
import ar.com.fiuba.domain.api.ErrorGenerator;

public class TestRunner {
	Corrector corrector;
	ErrorGenerator errorGenerator;
	String outputFileName;
	ArrayList<String> correctedWords;
	ArrayList<String> wordsToCorrect;
	ArrayList<String> expectedWords;
	double correctWords;
	int numberOfWords;
	public double errorAtTheBeginningPercent;
	public double errorInTheMiddlePercent;
	public double errorAtTheEndPercent;
    public double correctWordsBeginning;
    public double correctWordsMiddle;
    public double correctWordsEnd;
    FileWriter errorsStart; 
    FileWriter errorsMiddle; 
    FileWriter errorsEnd; 
	BufferedWriter outInsertionStart;
	BufferedWriter outInsertionMiddle;
	BufferedWriter outInsertionEnd;

    
    
	public TestRunner(Corrector corrector, int numberOfWords, ErrorGenerator errorGenerator, String outputFileName) throws IOException
	{
		this.corrector = corrector;
		this.errorGenerator = errorGenerator;
		correctedWords = new ArrayList<String>();
		expectedWords = errorGenerator.getCorrectWordsArray();
		this.numberOfWords = numberOfWords;
		errorsStart = new FileWriter(outputFileName + "Start.csv");
		errorsMiddle = new FileWriter(outputFileName + "Middle.csv");
		errorsEnd = new FileWriter(outputFileName + "End.csv");
		outInsertionStart = new BufferedWriter(errorsStart);
		outInsertionMiddle = new BufferedWriter(errorsMiddle);
		outInsertionEnd = new BufferedWriter(errorsEnd);

	}
	
	public void runTest() throws IOException{
		//Create array of correct words
		wordsToCorrect = errorGenerator.generateErrorAtTheBeginning();		
		int i = 0;
		for(String w:wordsToCorrect){
			String wordToCorrect = w;
			String corrected = corrector.getBestMatch(wordToCorrect);
			outInsertionStart.write(w + "," + corrected + "," + expectedWords.get(i) + "\n" );
			correctedWords.add(corrector.getBestMatch(wordToCorrect));
			i++;
		}
		errorAtTheBeginningPercent = getPercentageOfCorrectWords();
		correctWordsBeginning = correctWords;
		
		correctedWords = new ArrayList<String>();
		wordsToCorrect = errorGenerator.generateErrorInTheMiddle();		
		i = 0;
		for(String w:wordsToCorrect){
			String wordToCorrect = w;
			String corrected = corrector.getBestMatch(wordToCorrect);
			outInsertionMiddle.write(w + "," + corrected + "," + expectedWords.get(i) + "\n");
			correctedWords.add(corrected);
			i++;
		}	
		errorInTheMiddlePercent = getPercentageOfCorrectWords();
		correctWordsMiddle = correctWords;
		
		correctedWords = new ArrayList<String>();
		wordsToCorrect = errorGenerator.generateErrorAtTheEnd();		
		i = 0;
		for(String w:wordsToCorrect){
			String wordToCorrect = w;
			String corrected = corrector.getBestMatch(wordToCorrect);
			outInsertionEnd.write(w + "," + corrected + "," + expectedWords.get(i) + "\n");
			correctedWords.add(corrected);
			i++;
		}	
	
		errorAtTheEndPercent = getPercentageOfCorrectWords();
		correctWordsEnd = correctWords;
		
		outInsertionStart.close();
		outInsertionMiddle.close();
		outInsertionEnd.close();
		
	}
	
	//Returns the percentage of words that were corrected as expected. This means that the corrector returned the 
	//word in correctWords.
	public double getPercentageOfCorrectWords()
	{
		correctWords=0;
		double totalWords=this.numberOfWords;
		int i = 0;
		for (String s:correctedWords) {
			String correctWord = expectedWords.get(i); 
			if(correctWord.equals(correctedWords.get(i)))
				correctWords++;
			i++;
		}	
		return correctWords/totalWords;
	}
	
	
}
